﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Domain.MainModule.Entities;
using Infraestructure.CrossCutting.IoC;
using Infraestructure.CrossCutting.Logging;
using DistributedServices.Core.ErrorHandlers;
using System.ServiceModel;
using DistributedServices.MainModule.DTO;
using Application.MainModule.BankAccountsManagement;

namespace DistributedServices.MainModule
{
	public partial class MainModuleService
	{
        public List<BankAccount> GetBankAccountByUserId(Guid userId)
        {
            try
            {
                IBankAccountManagementService bankAccountService = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();

                return bankAccountService.FindBankAccountsByUserId(userId);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void ChangeBankAccount(BankAccount bankAccount)
        {
            try
            {
                IBankAccountManagementService bankAccountService = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();
                bankAccountService.ChangeBankAccount(bankAccount);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }

        }

        public void RemoveBankAccount(BankAccount bankAccount)
        {
            try
            {
                IBankAccountManagementService bankAccountService = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();
                bankAccountService.RemoveBankAccount(bankAccount);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public void AddBankAccount(BankAccount bankAccount)
        {
            try
            {
                IBankAccountManagementService bankAccountService = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();
                bankAccountService.AddBankAccount(bankAccount);
            }
            catch (ArgumentNullException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

        public List<BankAccount> GetPagedBankAccount(PagedCriteria pagedCriteria)
        {
            try
            {
                IBankAccountManagementService bankAccountService = IoCFactory.Instance.CurrentContainer.Resolve<IBankAccountManagementService>();

                return bankAccountService.FindPagedBankAccounts(pagedCriteria.PageIndex, pagedCriteria.PageCount);
            }
            catch (ArgumentException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
            catch (NullReferenceException ex)
            {
                ITraceManager traceManager = IoCFactory.Instance.CurrentContainer.Resolve<ITraceManager>();
                traceManager.TraceError(ex.Message);

                ServiceError detailedError = new ServiceError()
                {
                    ErrorMessage = Resources.Messages.exception_InvalidArguments
                };

                throw new FaultException<ServiceError>(detailedError);
            }
        }

	}
}
